<template>
  <div class="login-container">
    <div class="title-container">
      <h3 class="title">
          <!-- 换头部的图片 -->
        <img src="@/assets/common/theme.png" alt="">
      </h3>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
     <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.mobile"
          placeholder="请输入您的手机号"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入你的密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>

      <el-button class="loginBtns" 
      :loading="loading" 
      type="primary" 
      style="width:100%;"
       @click.native.prevent="handleLogin">登&emsp;&emsp;录
      </el-button>

    </el-form>
  </div>
</template>

<script>
// 一般大的项目会把很多的校验方法 放到一起
import { validMobile } from '@/utils/validate'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    // 自定义校验函数
    const validateMobile = function(rule, value, callback) {
      // 校验value
      // if (validMobile(value)) {
      //   // 如果通过 直接执行callback
      //   callback()
      // } else {
      //   callback(new Error('手机号格式不正确'))
      // }
      validMobile(value) ? callback() : callback(new Error('手机号格式不正确'))
    }
    return {
      loginForm: {
        mobile: '',
        password: ''
      },
      loginRules: {
        mobile: [{ required: true, trigger: 'blur', message: '不能为空' }, {
          validator: validateMobile, trigger: 'blur'
        }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }, {
          min: 6, max: 16, message: '密码的长度在6-16位之间 ', trigger: 'blur'
        }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 引入子模块方法
    ...mapActions(['user/login']),
    handleLogin() {
      // 登录按钮
      // 先完成手动的校验
      this.$refs.loginForm.validate(async isOK => {
        if (isOK) {
          try {
          // 表示校验通过
            this.loading = true // 先让按钮转圈
            await this['user/login'](this.loginForm) // 调用action
            this.loading = false
            // 跳转到主页
            this.$router.push('/') // 跳到主页
          } catch (error) {
            this.loading = false // 失败之后关闭转圈
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray: #000000;  // 将输入框颜色改成蓝色 // 光标颜色
$cursor: #fff;         

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url('~@/assets/common/bluetheme.png');
  background-position: center;
  .el-input {
    display: inline-block;
    height: 80px;
    width: 67%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 20px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 100%;
      caret-color: $light_gray;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
    }
  }

  .el-form-item {
    border: 1px solid #DCDCDC;
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 20px;
    color: #454545;
  }
   .el-form-item__error {
    color: #f00
  }
  .loginBtns {
  background: #4745C5;
  letter-spacing: 10px;
  height: 70px;
  text-align: center;
  line-height: 32px;
  font-size: 36px;
  border-radius: 20px;
  margin-top:75px;
}
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 750px;
    height: 450px;
    max-width: 100%;
    padding: 59px 125px 63px;
    margin: 0 auto;
    overflow: hidden;
    background-color: rgba(255, 255, 255, 0.55);
  }


  .svg-container {
    font-size: 50px;
    padding: 17px 10px 19px 23px;
    color: $dark_gray;
    vertical-align: middle;
    // width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      width: 540px;
      font-size: 26px;
      color: $light_gray;
      margin: 145px auto 90px auto;
      text-align: center;
      font-weight: bold;
      img {
        width: 100%;
      }
    }
  }
}
</style>
